package com.manlost.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

import com.manlost.web.common.ManLostSession;
import com.manlost.web.common.WebUser;
import com.manlost.web.common.cookie.MLostCookieKeyEnum;
import com.manlost.web.common.cookie.MLostCookieNameEnum;
import com.manlost.web.common.cookie.ManLostCookieManger;
import com.manlost.web.common.cookie.ManLostCookieSystemUtils;

/**
 * AccountInfoFilter 判断当前用户是否已经登录，若登录则build相应信息至WebUser对象
 * 
 * @author zhoukang
 * 
 */
public class AccountInfoFilter implements Filter {

	private FilterConfig filterConfig = null;

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		this.filterConfig = filterConfig;
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {

		cleanManLostSession();
		buildManLostSession((HttpServletRequest)request, (HttpServletResponse)response);
		chain.doFilter(request, response);
		saveManLostSession((HttpServletRequest)request);
		cleanManLostSession();
	}

	public void destroy() {
		this.filterConfig = null;
	}

	/**
	 * 清除线程缓存
	 */
	private void cleanManLostSession() {
		ManLostSession.clean();
	}

	/**
	 * 构建ManLostSession对象
	 * 
	 * @param request
	 */
	private void buildManLostSession(HttpServletRequest request, HttpServletResponse response) {
		if (ManLostSession.getCurrent() != null) {
			throw new RuntimeException("ManLostSession has already existed !!!");
		}

		ManLostSession session = new ManLostSession();
		ManLostSession.setCurrentSession(session);
		buildWebUser(request, response);
	}

	/**
	 * 若用户已经登录，构建WebUser对象
	 * 
	 * @param request
	 */
	private void buildWebUser(HttpServletRequest request, HttpServletResponse response) {
		WebUser webUser = new WebUser();
		ManLostSession.getCurrent().setWebUser(webUser);
		
		// create cookieManager and set in requset
		ManLostCookieManger cookieManager = new ManLostCookieManger(request, response);
		request.setAttribute(ManLostCookieManger.MANLOST_COOKIE_MANGER, cookieManager);
		
		boolean isSignIn = StringUtils.equals(cookieManager.getCookie(MLostCookieNameEnum.MLSC_TEMP, MLostCookieKeyEnum.IS_SIGN), "y");
		boolean isAutoSign = StringUtils.equals(cookieManager.getCookie(MLostCookieNameEnum.MLSC_FOREVER, MLostCookieKeyEnum.AUTO_SIGN), "y");
		
		// already sign in
		if (isSignIn) {
			setWebUserProperty(cookieManager, MLostCookieNameEnum.MLSC_TEMP, webUser);
	        return ;
		}
		
		// auto sign in
		if (isAutoSign) {
			setWebUserProperty(cookieManager, MLostCookieNameEnum.MLSC_FOREVER, webUser);
	        return ;
		}
	}

	/**
	 * 读取cookie中的属性设置到webUser中
	 * 
	 * @param cookieManager
	 * @param mlostCookieName
	 * @param webUser
	 */
	private void setWebUserProperty(ManLostCookieManger cookieManager, MLostCookieNameEnum mlostCookieName, WebUser webUser) {
		webUser.setSignIn(true);
		
		String username = cookieManager.getCookie(mlostCookieName, MLostCookieKeyEnum.ACCOUNT_NAME);
		webUser.setUsername(username);
		
		String accountId = cookieManager.getCookie(mlostCookieName, MLostCookieKeyEnum.ACCOUNT_ID);
		if(NumberUtils.isNumber(accountId)) {
			webUser.setId(NumberUtils.toLong(accountId));
		}
		
		String email = cookieManager.getCookie(mlostCookieName, MLostCookieKeyEnum.ACCOUNT_EMAIL);
		webUser.setEmail(email);
	}
	
	/**
	 * 保存cookie信息
	 * @param request
	 */
	private void saveManLostSession(HttpServletRequest request) {
		ManLostCookieManger cookieManager = ManLostCookieSystemUtils.getManager(request);
		cookieManager.saveAllCookie();
		
	}
}
